package mysql

import "gorm.io/gorm"

type User struct {
	gorm.Model
	Account  string `gorm:"type:varchar(20);not null;comment:'账号'"`
	Password string `gorm:"type:char(32);not null;comment:'密码'"`
	Mobile   string `gorm:"type:char(11);not null;comment:'密码'"`
}

type Activity struct {
	gorm.Model
	ActivityName string `gorm:"type:varchar(20);not null;comment:'活动名称'"`
	Address      string `gorm:"type:varchar(100);not null;comment:'活动地址'"`
	Time         string `gorm:"type:datetime;not null;comment:'活动地址'"`
	Name         string `gorm:"type:varchar(20);not null;comment:'主办人'"`
}

type Shop struct {
	gorm.Model
	UserId int     `gorm:"type:int;not null;comment:'用户id'"`
	Name   string  `gorm:"type:varchar(20);not null;comment:'商品名称'"`
	Price  float64 `gorm:"type:decimal(10,2);not null;comment:'商品价格'"`
	Stock  int     `gorm:"type:int;not null;comment:'库存数量'"`
}

type Order struct {
	gorm.Model
	UserId int     `gorm:"type:int;not null;comment:'用户id'"`
	ShopId int     `gorm:"type:int;not null;comment:'商品id'"`
	Name   string  `gorm:"type:varchar(20);not null;comment:'订单名称'"`
	Price  float64 `gorm:"type:decimal(10,2);not null;comment:'订单价格'"`
	Num    int     `gorm:"type:int;not null;comment:'订单数量'"`
}

func (s *Shop) CreateAnimals(db *gorm.DB) error {
	// 再唠叨一下，事务一旦开始，你就应该使用 tx 处理数据
	tx := db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return err
	}

	if err := tx.Create(&s).Error; err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}
